﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL; 
using KPIS.DBM;

using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public  class MoveRequestJobsiteBLL

    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
 


        public void Insert_hrm_es_move_requests(int emp_seq, int created_by)
        {

            try
            {

                dbManager.Open();
                dbManager.CreateParameters(5);
                dbManager.AddParameters(0, "@emp_seq", emp_seq);
                dbManager.AddParameters(1, "@approved_status", SystemInfo._ApproveRecordStatus);
                dbManager.AddParameters(2, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(3, "@created_by", created_by);
                dbManager.AddParameters(4, "@created_when", DateTime.Today);

                string strSQL;

                strSQL = " insert into hrm_es_move_requests(emp_seq,approved_status,record_status,created_by,created_when)"
                 + " values(@emp_seq, @approved_status, @record_status, @created_by,@created_when)";


                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                

            }
            catch (Exception ex)
            {
                throw new Exception("{Insert_hrm_es_move_requests}" + ex.Message);
            }
            finally
            {
                dbManager.Dispose();
            }

        }


        public void Insert_hrm_es_move_request_dtls(int req_seq,int seq,int job_site_seq,string reason, int created_by)
        {

            try
            {


                dbManager.Open();
                dbManager.CreateParameters(7);
                dbManager.AddParameters(0, "@req_seq", req_seq);
                dbManager.AddParameters(1, "@seq", seq);
                dbManager.AddParameters(2, "@job_site_seq", job_site_seq);
                dbManager.AddParameters(3, "@reason", reason);
                dbManager.AddParameters(4, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@created_by", created_by);
                dbManager.AddParameters(6, "@created_when", DateTime.Today);


                string strSQL;
                strSQL = " insert into hrm_es_move_request_dtls(req_seq,seq,job_site_seq,reason,record_status,created_by,created_when)"
                 + " values(@req_seq,@seq,@job_site_seq,@reason,@record_status,@created_by,@created_when)";


                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                

            }
            catch (Exception ex)
            {
                throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }

        }
        public void Update(int req_dtls_seq, int seq, int job_site_seq, string reason, int updated_by)
        {
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@job_site_seq", job_site_seq);
                dbManager.AddParameters(2, "@reason", reason);
                dbManager.AddParameters(3, "@updated_by", updated_by);
                dbManager.AddParameters(4, "@req_dtls_seq", req_dtls_seq);
                dbManager.AddParameters(5, "@updated_when", DateTime.Today);

                string strSQL;
                strSQL = " update hrm_es_move_request_dtls "
                + " set seq=@seq,"
                + " job_site_seq=@job_site_seq,"
                + " reason=@reason,"
                + " updated_by=@updated_by ,"
                + " updated_when=@updated_when"
                + " where req_dtls_seq = @req_dtls_seq ";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);

            }
            catch (Exception ex)
            {
                throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int req_seq,int UpdatedBy)
        {
            try
            {

                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@updated_by", UpdatedBy);
                dbManager.AddParameters(2, "@req_seq", req_seq);

                string sql;
                sql = " update hrm_es_move_request_dtls set "
                + " record_status=@record_status"
                + ", updated_by = @updated_by"
                  + " where req_dtls_seq = @req_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }
        }



        public DataTable   select(int emp_seq)
        {

            try
            {
                string sql;
                sql = "select dtl.reason as reason,dtl.seq as seq,dtl.req_dtls_seq as id,dtl.job_site_seq as job_site_seq, "
             + " job.job_site_name as job_name "
             + " from hrm_es_move_request_dtls as dtl,"
             + " hrm_mp_job_sites as job "
             + " where dtl.record_status='U' and dtl.job_site_seq = job.job_site_seq "
             + " and dtl.req_seq =  " + emp_seq + " order by seq asc";
                DataSet ds;
                DataTable dt;
                ds = new DataSet();
                dbManager.Open();
                ds = dbManager.ExecuteDataSet(CommandType.Text, sql);
                dt = ds.Tables[0];
                return dt;
            }
            catch(Exception ex)
            {

                return null;
                // throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();  
            }
        }


        public int get_req_seq(int emp_seq)
        {
            string sql;
            sql = "select max(req_seq) from hrm_es_move_requests where emp_seq=" + emp_seq;
            try
            {
                object obj;
                dbManager.Open();
                obj = dbManager.ExecuteScalar(CommandType.Text, sql);
                return (int)obj + 1;
            }
            catch
            {
                return 1;
                // throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();  
            }
        }

        public int getSeq(int emp_seq)
        {
            string sql;
            sql = "select max(seq) from hrm_es_move_request_dtls where emp_seq=" + emp_seq ;
            try
            {
                object obj;
                dbManager.Open();
                obj = dbManager.ExecuteScalar(CommandType.Text, sql);
                return (int)obj + 1;

            }
            catch (Exception Ex)
            {
                return 1;
                // throw new Exception("");
            }
            finally
            {
                dbManager.Dispose();
            }
        }



    }
}
